#include "myboard.h"
#include <string.h>

#include "cbb_event.h"
#include "cbb_init.h"

#include "bsp_gpio.h"
#include "bsp_uart_dma.h"
#include "bsp_tim.h"

// #include "bsp_spi.h"

#include "device.h"
#include "led.h"
#include "button.h"
#include "uart.h"
// #include "w25qxx.h"

// 设备枚举
Device_t* pstLedBlue;
Device_t* pstLedGreen;
Device_t* pstBtnMode;
Device_t* pstBtnUpgrade;
Device_t* pstUsart1;
// Device_t* pstW25qxx;

// 事件枚举
Event_t* pstEventTimLed;
Event_t* pstEventBtnMode;
Event_t* pstEventBtnUpgrade;

// 定时器枚举
Soft_Tim_t* pstSoftTimLed;

void tim_tick(void* pvArgs){
    (void) pvArgs;
    cbb_soft_tim_monitor(pstSoftTimLed);
}

static void led_init(void){

    Led_t led_blue = {
        .HW = {
            .pfnWrite = bsp_blue_led_write,
        },
        .SW = {
            .u8Active = IO_LOW,
            .u8State = IO_HIGH
        }
    };
    pstLedBlue = led_register(&led_blue);        

    Led_t led_green = {
        .HW = {
            .pfnWrite = bsp_green_led_write,
        },
        .SW = {
            .u8Active = IO_LOW,
            .u8State = IO_HIGH,
        }
    };
    pstLedGreen = led_register(&led_green);    
        
}
INIT_PORT_DEVICE(led_init);
 
static void button_init(void){

    Button_t button_mode =  {
        .HW = {
            .pfnRead = bsp_mode_btn_read,
        },
        .SW = {
            .u8Active = IO_LOW,
            .u8State = IO_HIGH,
        }
    };
    pstBtnMode = button_register(&button_mode);

    Button_t button_upgrade = {
        .HW = {
            .pfnRead = bsp_upgrade_btn_read,
        },
        .SW = {
            .u8Active = IO_LOW,
            .u8State = IO_HIGH,
        }
    };
    pstBtnUpgrade = button_register(&button_upgrade);
}
INIT_PORT_DEVICE(button_init);

static void uart_init(void){

    // 串口接收缓存
    static uint8_t u8RxData[1032];

    Uart_t uart1 = {
        .HW = {
            .pfnReceive = uart1_dma_receive,
            .pfnTransmit = uart1_dma_transmit,
        },
        .SW = {
            .pu8Data = u8RxData,
            .u32Len = sizeof(u8RxData),
            .u32Head = 0,
            .u32Tail = 0,
            .u32Index = 0,
            .u32TimeOut = 5,
        }
    };
    pstUsart1 = uart_register(&uart1);
}
INIT_PORT_DEVICE(uart_init);

void myboard_init(void){


    // 其余设备初始化操作
    // static uint8_t u8SecCache[SECTOR_SIZE];
    // W25QXX_t w25qxx = {
    //     .HW = {
    //         .pfnCS = spi3_cs,
    //         .pfnTransRecv = spi3_transmit_receive
    //     },
    //     .SW = {
    //         .u8CSActive = IO_LOW,
    //         .u32RetryNum = 1000,
    //         .pu8SecCache = u8SecCache,
    //     }
    // };

    // pstW25qxx = w25qxx_register(&w25qxx);

    // 注册定时器
    pstSoftTimLed = cbb_soft_tim_register(1000);
    cbb_soft_tim_enable(pstSoftTimLed,1);

    // 注册事件
    pstEventTimLed = cbb_event_register(&(pstSoftTimLed->u16Flag),EVENT_16,SOFT_TIM_TIMOUT);
    pstEventBtnMode = cbb_event_register(&(pstBtnMode->u32Event), EVENT_32, BUTTON_CLICKED);
    pstEventBtnUpgrade = cbb_event_register(&(pstBtnUpgrade->u32Event), EVENT_32, BUTTON_CLICKED); 

    // 注册定时器事件
    cbb_event_connect(pstEventTim6, tim_tick, NULL);

}
INIT_PORT_DEVICE(myboard_init);
